package com.medicine.hbp.fragment.myuser;

import org.kymjs.kjframe.http.HttpParams;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.TextView;

import com.google.gson.Gson;
import com.medicine.hbp.R;
import com.medicine.hbp.adapter.ClassificationAdapter;
import com.medicine.hbp.adapter.ClassificationDetailAdapter;
import com.medicine.hbp.adapter.FragmentAdapter03;
import com.medicine.hbp.http.AppHttpCallBack;
import com.medicine.hbp.http.AppHttps;
import com.medicine.hbp.http.HttpInterface;
import com.medicine.hbp.mob.UserContributeVo;
import com.medicine.hbp.sp.DadosSp;
import com.medicine.hbp.sp.LoginSp;
import com.medicine.hbp.uitl.CommUtil;
import com.medicine.hbp.uitl.ToastUtil;
import com.medicine.hbp.xListView.XListView;
import com.medicine.hbp.xListView.XListView.IXListViewListener;

/**
 * 新收藏界面
 */
public class FragmentUser01 extends Fragment implements OnItemClickListener, IXListViewListener {
	private View Views;
	private XListView xlistview;
	private ClassificationDetailAdapter adapter;
	private volatile boolean refresh = true;
	private volatile int page = 1;// 当前页（从0开始）
	private int pageSize = 10;
	private int userid=-1;
	private Gson gson;
	private Bundle bundle;
	private TextView Nodate;
	@Override
	public android.view.View onCreateView(LayoutInflater inflater,
										  ViewGroup container, Bundle savedInstanceState) {
		Views = inflater.inflate(R.layout.fragment_user_04, container, false);
		xlistview = (XListView) Views.findViewById(R.id.xListView);
		Nodate = (TextView) Views.findViewById(R.id.no_date);
		initView();
		return Views;
	}

	private void initView() {
		try {
			bundle=getArguments();
			userid=bundle.getInt("userId");
		} catch (Exception e) {
		}
		gson=new Gson();
		adapter = new ClassificationDetailAdapter(getActivity());
		xlistview.setAdapter(adapter);
		xlistview.setOnItemClickListener(this);
		xlistview.setXListViewListener(this);
		DadosSp.setAdapter(getActivity(), LoginSp.getUserId(getActivity())+"", adapter);
		httpUserCollectList();
	}
	/**
	 * 用户获取自己收藏数据
	 */
	private void httpUserCollectList() {
		HttpParams params = new HttpParams();
		params.put("current", page);// 当前页数(不传默认为1)
		params.put("rowCount", pageSize);// 每页显示数(不传默认为10)
		if(userid!=-1){
			params.put("otherUserId", userid);// 其他用户id
		}
		AppHttps.HttpPost(getActivity(),HttpInterface.USER_COLLECT_LIST, params,
				new AppHttpCallBack<UserContributeVo>(UserContributeVo.class,getActivity()) {
					@Override
					public void onSuccess(UserContributeVo result) {
						boolean hasMore = true;
						if (refresh) {
							if(result.artInfo.size()>0){
								DadosSp.setClassificationDetail(getActivity(), LoginSp.getUserId(getActivity())+"", gson.toJson(result));
								adapter.changeData(result.artInfo);
								xlistview.setVisibility(View.VISIBLE);
								Nodate.setVisibility(View.GONE);
							}else{
								xlistview.setVisibility(View.GONE);
								Nodate.setVisibility(View.VISIBLE);
							}
						} else {
							adapter.addItems(result.artInfo);
							adapter.notifyDataSetChanged();
						}
						if (result.artInfo.size() < pageSize) {
							hasMore = false;
						}
						xlistview.setPullLoadEnable(hasMore);
					}
					@Override
					public void onFailures(int errorCode, String errorMsg) {
						ToastUtil.showShort(getActivity(), errorMsg);
					}

					@Override
					public void onFinish() {
						super.onFinish();
						xlistview.setRefreshTime(CommUtil.formatDate(System.currentTimeMillis()));
					}
				});
	}

	@Override
	public void onRefresh() {
		page=1;
		refresh = true;
		xlistview.setPullLoadEnable(false);
		httpUserCollectList();
	}
	@Override
	public void onLoadMore() {
		page++;
		refresh = false;
		httpUserCollectList();
	}
	@Override
	public void onItemClick(AdapterView<?> arg0, android.view.View arg1,
							int arg2, long arg3) {
		// TODO Auto-generated method stub

	}

}